package com.flower.controller;


import com.flower.pojo.Flower;
import com.flower.pojo.Store;
import com.flower.pojo.User;
import com.flower.service.StoreService;
import com.flower.service.UserLoginService;
import com.flower.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.Sides;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 控制层
 */
@RestController
@RequestMapping("/store")
public class StoreController {
    @Autowired
    private StoreService StoreService;

    //商家登录
    @RequestMapping("/storelogin.do")
    public Map<String, Object> loginStore(String account, String password, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Store store = StoreService.LoginStoreByAccount(account);
        if (store == null) {
            map.put("code", 201);
            map.put("msg", "商家不存在");
        } else if (!password.equals(store.getPassword())) {
            map.put("code", 202);
            map.put("msg", "密码错误");
        } else {
            map.put("code", 200);
            map.put("msg", "登录成功");
            map.put("user", store);
        }
        return map;
    }

    //商家注册
    @RequestMapping("/storeregister.do")
    public Map<String, Object> registerStore(Store store, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        int result = StoreService.regiterStore(store);

        if (result != -1) {
            map.put("code", 200);
            map.put("msg", "商家添加成功");
        } else {
            map.put("code", 201);
            map.put("code", "商家添加失败");
        }
        return map;
    }


    //    自动注入
    @Autowired
    private StoreService storeService;

    /**
     * 根据id查询商家信息
     *
     * @param sid
     * @return
     */
    @RequestMapping(value = "/query_Store.do/{sid}", method = RequestMethod.GET)
    public Map<String, Object> queryById(@PathVariable("sid") Integer sid) {
        Map<String, Object> map = new HashMap<>();
        Store store = new Store();
        store = storeService.queryById(sid);
        if (store == null) {
            map.put("code", 201);
            map.put("msg", "查询失败");
        } else {
            map.put("code", 200);
            map.put("msg", "查询信息成功");
            map.put("data", store);
        }
        return map;
    }

    /**
     * 商家修改信息
     */
    @RequestMapping("/update_store.do")
    public Map<String, Object> updateStore(@RequestBody Store store) {
        Map<String, Object> map = new HashMap<>();
        int result = storeService.updateShore(store);
        if (result != -1) {
            map.put("code", 200);
            map.put("msg", "修改信息成功");
        } else {
            map.put("code", 201);
            map.put("msg", "修改信息失败");
        }
        return map;
    }

    /**
     * 商家修改密码
     *
     * @param
     * @return
     */
    @RequestMapping("/update_storePs.do")
    public Map<String, Object> updateStorePassword(@RequestBody Map<String, Object> map1) {
        Map<String, Object> map = new HashMap<>();
        Store store = new Store();
        store.setSid(Integer.parseInt(map1.get("sid").toString()));
        store.setPassword(map1.get("password").toString());
        int result = storeService.updateShorePs(store);
        if (result != -1) {
            map.put("code", 200);
            map.put("msg", "修改密码成功");
        } else {
            map.put("code", 201);
            map.put("msg", "修改密码失败");
        }
        return map;
    }

    @RequestMapping("/query_store_flower")
    public Map<String, Object> querystore_flower(int id) {
        Map<String, Object> map = new HashMap<>();
        List<Flower> list = storeService.query_all_flower(id);
        if (list != null && !list.isEmpty()) {
            map.put("code", "200");
            map.put("msg", "查询商家鲜花成功");
            map.put("list", list);
        } else {
            map.put("code", "201");
            map.put("msg", "查询商家鲜花失败");
        }
        return map;
    }


}
